package com.chance.cc.crawler.development.bootstrap.weibo;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.downloader.proxy.Proxy;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRecord;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.controller.DevCrawlerController;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRecordFilter.dateRange;
import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;
/**
 * @Author Zhao.Hhuan
 * @Date Create in 2020/12/28 13:31
 * @Description
 *      微博关键词搜索启动
 **/
public class WeiboSearchKwStart {
    public static final String domainId = "weibo";
    public static final String site = "searchKw";

    private static Proxy proxy = new Proxy();
    static {
        //代理配置
        proxy.setHost("http-dyn.abuyun.com");
        proxy.setPort(9020);
        proxy.setUsername("HL89Q19E86E2987D");
        proxy.setPassword("71F33D94CE5F7BF2");
    }

    public static void main(String[] args) {
//        searchKwRecord();
        searchKwRecordOne();
    }

    private static void  searchKwRecord(){
        String searchUrl = "https://s.weibo.com/?Refer=";
        String siteBiz = "self_searchKw-realtime";
        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domainId, turnPageItem)
                .httpUrl(searchUrl)
                .releaseTime(System.currentTimeMillis())
                .filter(CrawlerEnum.CrawlerRecordFilter.keyOrDateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24 * 1,null))
                .addFilterInfo(FilterUtils.memoryFilterKeyInfo(StringUtils.joinWith("-","filter",domainId,site,siteBiz,"queue")))
                .proxy(proxy)
                .needWashed(false)
                .needParsed(false)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.interaction)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.comment)
                .build();
        requestRecord.setDownload(false);
        requestRecord.setSkipPipeline(true);
        requestRecord.tagsCreator().bizTags().addDomain(domainId);
        requestRecord.tagsCreator().bizTags().addSite(site);
        requestRecord.tagsCreator().bizTags().addSiteBiz(siteBiz);

        CrawlerRecord commentFilter = new CrawlerRequestRecord();
        commentFilter.setFilter(dateRange);
//        commentFilter.addFilterInfo(FilterUtils.memoryFilterKeyInfo(StringUtils.joinWith("-","filter",domainId,site,"queue")));
        commentFilter.addFilterInfo(FilterUtils.dateRangeFilterInfo(24,null));
        requestRecord.tagsCreator().resultTags().getCategoryTag().addKVTag("comment_filter_record", JSON.toJSONString(commentFilter));
        requestRecord.getHttpRequest().addExtra("searchKwSourceUrl","https://s.weibo.com/weibo?q=%s&typeall=1&suball=1&timescope=custom:2020-12-01:&Refer=g");


        //keyword record
        CrawlerRequestRecord keywordRecord = CrawlerRequestRecord.builder()
                .startPageRequest("weibo_search_kw",turnPageItem)
                .httpUrl("http://192.168.1.215:9599/v1/meta/"+domainId+"/keys?site=" + site)
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        //user oauth info source
        CrawlerRequestRecord userOauthInfoCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("weibo_user_oauth_infos",turnPageItem)
                .httpUrl("http://192.168.1.215:9599/crawler/oauth/api/v1/"+domainId+"/userOauthInfos")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        //user agents
        CrawlerRequestRecord userAgentsCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("weibo_user_agents", turnPageItem)
                .httpUrl("https://fake-useragent.herokuapp.com/browsers/0.1.11")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();


        DevCrawlerController.builder()
                .triggerInfo(domainId,domainId,System.currentTimeMillis(),domainId)
                .crawlerRequestQueue(DevCrawlerController.devRequestQueue(domainId))
                .consoleResultPipeline()
//                .fileResultPipeline("D:\\chance\\log\\weiboSearchKw.log",false)
                .requestRecord(requestRecord)
//                .supportRecord(keywordRecord)
//                .supportRecord(userAgentsCrawlerRecord)
//                .supportRecord(userOauthInfoCrawlerRecord)
                .build().start();
    }

    private static void  searchKwRecordOne(){
        String searchUrl = "https://s.weibo.com/weibo?q=%E6%B5%99%E6%B1%9F%E5%A4%A7%E5%AD%A6&typeall=1&suball=1&timescope=custom:2020-12-01:&Refer=g";
        String siteBiz = "self_searchKw-realtime";
        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domainId, turnPageItem)
                .httpUrl(searchUrl)
                .releaseTime(System.currentTimeMillis())
                .filter(CrawlerEnum.CrawlerRecordFilter.keyOrDateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24 * 1,null))
                .addFilterInfo(FilterUtils.memoryFilterKeyInfo(StringUtils.joinWith("-","filter",domainId,site,siteBiz,"queue")))
                .proxy(proxy)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.interaction)
                .build();;
        requestRecord.tagsCreator().bizTags().addDomain(domainId);
        requestRecord.tagsCreator().bizTags().addSite(site);
        requestRecord.tagsCreator().bizTags().addSiteBiz(siteBiz);

        CrawlerRecord commentFilter = new CrawlerRequestRecord();
        commentFilter.setFilter(dateRange);
//        commentFilter.addFilterInfo(FilterUtils.memoryFilterKeyInfo(StringUtils.joinWith("-","filter",domainId,site,"queue")));
        commentFilter.addFilterInfo(FilterUtils.dateRangeFilterInfo(24,null));
        requestRecord.tagsCreator().resultTags().getCategoryTag().addKVTag("comment_filter_record", JSON.toJSONString(commentFilter));

        //user oauth info source
        CrawlerRequestRecord userOauthInfoCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("weibo_user_oauth_infos",turnPageItem)
                .httpUrl("http://192.168.1.215:9599/crawler/oauth/api/v1/"+domainId+"/userOauthInfos")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        //user agents
        CrawlerRequestRecord userAgentsCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("weibo_user_agents", turnPageItem)
                .httpUrl("https://fake-useragent.herokuapp.com/browsers/0.1.11")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();


        DevCrawlerController.builder()
                .triggerInfo(domainId,domainId,System.currentTimeMillis(),domainId)
                .crawlerRequestQueue(DevCrawlerController.devRequestQueue(domainId))
                .consoleResultPipeline()
//                .fileResultPipeline("D:\\chance\\log\\weiboSearchKw.log",false)
                .requestRecord(requestRecord)
                .supportRecord(userAgentsCrawlerRecord)
                .supportRecord(userOauthInfoCrawlerRecord)
                .build().start();
    }

}
